package top.xiaoshuaila.moments.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.util.DigestUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.xiaoshuaila.moments.pojo.RespBean;
import top.xiaoshuaila.moments.pojo.Users;
import top.xiaoshuaila.moments.service.LoginService;
import top.xiaoshuaila.moments.utils.Dateutils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.UUID;

@Controller
@Api(tags = "登录注册接口")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @PostMapping("/loginin")
    @ResponseBody
    @ApiOperation("用户登录")
    public RespBean loginin(String username, String password, HttpServletRequest request){
        return loginService.login(username,password,request);
    }
    @PostMapping("regist")
    @ResponseBody
    @ApiOperation("用户注册")
    public RespBean regist(String rn, String un, String pw1, String pw2, String intro, MultipartFile file){
        System.out.println(rn+":"+un+":"+pw1+":"+pw2+":"+intro+":"+intro+":"+file);
        if(rn.equals(""))rn="wms"+UUID.randomUUID().toString().replaceAll("-","").substring(0,5);
        if(un.equals(""))return RespBean.error("密码不能为空！");
        if(loginService.containusername(un))return RespBean.error("用户名已存在！");
        if(pw1.equals(""))return RespBean.error("密码不能为空！");
        if(!pw1.equals(pw2))return RespBean.error("两次密码输入不一致！");
        if(intro.equals(""))intro="这个人一句话都没说。";
        String headphoto="";
        if(file!=null)
        {
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf('.'));
            String newFileName = UUID.randomUUID().toString().replace("-","") + suffix;
            String path = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/image/";
            System.out.println(path);
            File newFile = new File(path + newFileName);
            try {
                file.transferTo(newFile);
            } catch (Exception e) {
                e.printStackTrace();
                RespBean.error("发生失败");
            }
            headphoto+="image/"+newFileName;
        }
        else headphoto="image/head/("+new Random().nextInt(15)+").png";
        loginService.regist(un,rn, DigestUtils.md5DigestAsHex(pw1.getBytes()),intro,headphoto, Dateutils.getDatetime());
        return RespBean.success("注册成功！");
    }
    @GetMapping("loginInfo")
    @ResponseBody
    @ApiOperation("获取个人信息")
    public RespBean loginInfo(HttpServletRequest request){
        return loginService.loginInfo(request);
    }

}
